﻿using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

namespace ns
{
    public class ConfigurationReader 
    {
        public static string GetConfigFile(string filename)
        {
            string url;
            url = Application.dataPath + "/StreamingAssets/" + filename;
            #region
#if UNITY_EDITOR
            url = Application.dataPath + "/StreamingAssets/" + filename;
#elif UNITY_WINDOW
            url = Application.dataPath + "/StreamingAssets/" + filename;            
#elif UNITY_IOS
            url = Application.dataPath + "/Raw/"+filename; 
#elif UNITY_ANDROID
            url = Application.dataPath + "!/assets/"+filename; 
#endif
            #endregion
            UnityWebRequest www = UnityWebRequest.Get(url);
            www.SendWebRequest();   //发送请求，这一步真的很重要
            while (www.result != UnityWebRequest.Result.Success) { }
            return www.downloadHandler.text;
        }

        public static void Reader(string fileContent,Action<string> handler)
        {
            using (StringReader reader = new StringReader(fileContent))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    handler(line);
                }
            }
        }
    }
}